package pe.upc.dsd.jjjar.metropolitano;

import java.util.List;

import pe.upc.dsd.jjjar.metropolitano.services.RequestMethod;
import pe.upc.dsd.jjjar.metropolitano.services.RestClient;
import pe.upc.dsd.jjjar.rest.informacion.Estacion;
import pe.upc.dsd.jjjar.rest.informacion.Response;
import pe.upc.dsd.jjjar.rest.recarga.Respuesta;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RecargaActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recarga);

		final EditText txtNroTarjeta = (EditText) findViewById(R.id.txtNroTarjeta);
		final EditText txtMonto = (EditText) findViewById(R.id.txtMonto);
		Button btnRecarga = (Button) findViewById(R.id.btnRecargar);
		btnRecarga.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// http://localhost:8080/Recarga/rest/recarga/recargar/1000/15
				RestClient client = new RestClient(
						Constantes.SERVER_ENDPOINT + "/Recarga/rest/recarga/recargar/"
								+ txtNroTarjeta.getText().toString() + "/"
								+ txtMonto.getText().toString());
				try {
					client.Execute(RequestMethod.GET);
				} catch (Exception e) {

				}

				String response = client.getResponse();

				Gson gson = new Gson();
				Respuesta responseObject = null;

				try{
					responseObject = gson.fromJson(response, Respuesta.class);
				}catch(JsonParseException e){}

				if (responseObject != null) {

					String estado = responseObject.getEstado();

					if (estado.equals("OK")) {

						Intent recargaIntent = new Intent(RecargaActivity.this,
								ConfirmationActivity.class);
						recargaIntent.putExtra("nrooperacion",
								responseObject.getNroOperacion());
						startActivity(recargaIntent);

					} else {

						Toast.makeText(getApplicationContext(),
								responseObject.getMensaje(), Toast.LENGTH_SHORT)
								.show();

					}

				} else {

					AlertDialog.Builder builder = new AlertDialog.Builder(
							RecargaActivity.this);
					builder.setMessage(
							"Se produjo un error en la comunicacion con el WebService.")
							.setCancelable(false)
							.setPositiveButton("Yes",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											RecargaActivity.this.finish();
										}
									})
							.setNegativeButton("No",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
										}
									});
					AlertDialog alert = builder.create();

				}

			}
		});

	}

}
